home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: getchar() problems!
- Date: 13 Jan 1996 17:30:45 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d8q85$mqc@news.iag.net>
- References: <DL2wFD.8BK@cdf.toronto.edu>
- NNTP-Posting-Host: pm2-orl20.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <DL2wFD.8BK@cdf.toronto.edu>, a228lave@cdf.toronto.edu says...
-
- <Sorry to have snipped meaningful code, but IAG has decided to enforce that
- blasted quoted to new lines ration. I'll have to find a new ISP.>
-
- > printf("Please enter a series of chars. Type '#' to quit.\n");
- > while ((ch=getchar()) != '#')
- > {
- > switch (ch)
- > {
- > case SPACE: sp_c++; break;
- > case NL : nl_c++; break;
- > default : ot_c++;
- > }
- > }
- > printf("There were %d spaces, %d newlines, and %d other
- >chars!",sp_c,nl_c,ot_c);
- > ch=getchar();
- >}
- >
- >}
- >
- >What _actually_ happens is the code takes characters until you hit the #, and
- >then continues until you press return. Also, the final ch=getchar(); line
- >appears to be ignored... it's intended as a pause. I'm somewhat of a
- beginner
- >to the language, so any help will be greatly appreciated!
-
- Your problems are arising from the fact that keyboard input is line bufferred.
- No input is fed to your code until the user pressed return. Then the entire
- line is made available. In your code, getchar() reads one char at a time
- from the buffer, until it reads a '#'. At that point it exits the loop and
- prints the stats. When you call the last getchar() (for your pause), it
- simply reads the next char in the same buffer (you know that there is at
- least a '\n' left in it). So, you don't see a pause.
-
- The c.l.c faq (Frequently Asked Question) list has a very good explanation
- of this and several possible solutions (all are system specific. There is
- no ansi solution, if you want unbufferred input) in Q19.1. It is available
- for anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-